package cn.how2go.api.controller;

import cn.how2go.api.common.CommonResult;
import cn.how2go.api.common.MessageResult;
import cn.how2go.api.entity.Person;
import cn.how2go.api.service.PersonService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2020-05-31
 */
@Api(value = "人员",description = "人员信息的管理")
@RestController
@RequestMapping("/api/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    @ApiOperation(value="查询人员信息列表", notes="")
    @RequestMapping(value = "listPerson", method = RequestMethod.GET)
    public CommonResult listPerson(){
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("state",1).or().eq("state",2);
        entityWrapper.orderBy("CreateTime",false);
        List<Person> list = personService.selectList(entityWrapper);
        return CommonResult.success(list);
    }

    @ApiOperation(value="保存人员信息", notes="添加一名新人员信息，CreateTime后台生成")
    @RequestMapping(value = "savePerson", method = RequestMethod.POST)
    public CommonResult savePerson(@RequestBody Person person){
        person.setId(UUID.randomUUID().toString());
        person.setCreateTime(new Date());
        boolean flag = personService.insert(person);
        if(!flag){
            return CommonResult.fail("保存人员信息失败");
        }
        return CommonResult.success(person);
    }

    @ApiOperation(value="修改人员信息", notes="根据id修改人员信息")
    @RequestMapping(value = "editPerson", method = RequestMethod.POST)
    public CommonResult editPerson(@RequestBody Person person){
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("ID",person.getId());
        boolean flag = personService.update(person,entityWrapper);
        if(!flag) {
            return CommonResult.fail("修改失败");
        }
        return CommonResult.success(person);
    }

    @ApiOperation(value="获取人员信息", notes="根据ID编号获取人员信息")
    @RequestMapping(value = "getPersonById",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "ID", value = "UUID", required = true, dataType = "String"),
    })
    public CommonResult getPersonById(@RequestParam(value = "ID") String id){
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("ID",id);
        Person person = personService.selectOne(entityWrapper);
        if(person == null){
            return CommonResult.fail("获取失败");
        }
        return CommonResult.success(person);
    }

    @ApiOperation(value="软删除人员信息", notes="根据ID编号删除人员信息")
    @RequestMapping(value = "deleteById",method = {RequestMethod.POST,RequestMethod.GET})
    public MessageResult deleteById(@RequestParam String id){
        EntityWrapper<Person> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("ID",id);
        Person person = new Person();
        person.setState(3);
        boolean flag = personService.update(person,entityWrapper);
        if(!flag){
            return MessageResult.getError("500","删除失败");
        }
        return MessageResult.getSuccess("删除成功");
    }
}

